package com.ivan.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivan.common.result.Result;
import com.ivan.model.entity.acl.User;
import com.ivan.model.entity.user.UserInfo;
import com.ivan.model.vo.user.UserInfoQueryVo;
import com.ivan.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Author      Ivan
 * @Date        2023/4/6 14:27
 * @Description UserInfoController
 */
@Api(tags = "后台用户信息管理接口")
//@CrossOrigin//使用gateway后做了配置
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "条件查询用户信息带分页")
    @GetMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable("current") long current,
                             @PathVariable("limit") long limit,
                             UserInfoQueryVo queryVo) {
        Page<UserInfo> page = userInfoService.findByPage(current, limit, queryVo);
        return Result.ok(page);
    }

    @ApiOperation(value = "根据id查询用户信息")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Long id) {
        UserInfo userInfo = userInfoService.findById(id);
        return Result.ok(userInfo);
    }

    @ApiOperation(value = "根据id查询用户详情信息(包括就诊人)")
    @GetMapping("/getUserDetail/{id}")
    public Result getUserDetail(@PathVariable("id") Long id) {
        Map<String, Object> map = userInfoService.getUserDetail(id);
        return Result.ok(map);
    }

    @ApiOperation(value = "添加用户信息")
    @PostMapping("/saveUserInfo")
    public Result saveUserInfo(@RequestBody UserInfo userInfo) {
        int save = userInfoService.saveUserInfo(userInfo);
        if (save > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id删除用户信息")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        int delete = userInfoService.removeById(id);
        if (delete > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据idList批量删除用户信息")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> idList) {
        int delete = userInfoService.removeByIds(idList);
        if (delete > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfo userInfo) {
        int update = userInfoService.updateUserInfo(userInfo);
        if (update > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id锁定用户")
    @GetMapping("/lockById/{id}/{status}")
    public Result lockById(@PathVariable("id") Long id,
                           @PathVariable("status") Integer status) {
        int update = userInfoService.lockById(id, status);
        if (update > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }







    @ApiOperation(value = "后台系统登录")
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
//        System.out.println(user);
        Map<String, Object> data = new HashMap<>();
        data.put("token", "123456789");
        return Result.ok(data);
    }

    @ApiOperation(value = "后台系统获取用户信息")
    @GetMapping("/info")
    public Result info(@RequestParam String token) {
//        System.out.println(token);
        Map<String, Object> data = new HashMap<>();
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        data.put("introduction", "I am a super administrator");
        data.put("name", "Ivan");
        return Result.ok(data);
    }

    @ApiOperation(value = "后台系统退出登录")
    @PostMapping("/logout")
    public Result logout() {
        return Result.ok("success");
    }

}
